package com.lc.demo.service.impl;

import com.lc.demo.bean.Cart;
import com.lc.demo.mapper.CartMapper;
import com.lc.demo.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: sakila
 * @Date: 2022/03/07/19:32
 * @Description:
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    public CartMapper cartMapper;

    @Override
    public List<Cart> getCartList(Integer id) {
        return cartMapper.getCartList(id);
    }

    @Override
    public void changeCart(Cart cart, Integer id) {
        //判断当前用户购物车中是否有该商品

        Cart cartQuery = cartMapper.selectGood(cart.getGoodsId(), id);

        //如果没有 添加购物车
        if (cartQuery == null) {
            Cart cartInsert = new Cart();
            cartInsert.setGoodsNum(cart.getGoodsNum());
            cartInsert.setGoodsId(cart.getGoodsId());
            cartInsert.setUserId(id);
            cartMapper.insert(cartInsert);
        } else {
            //如果有 将goods的数量改变
            cartMapper.changeCart(cart.getGoodsNum(), cart.getGoodsId(), id);
            Cart cartSelect = new Cart();
            cartSelect.setUserId(id);
            cartSelect.setGoodsId(cart.getGoodsId());
            //如果数量为O 删除该数据
            Cart cart1 = cartMapper.selectOne(cartSelect);
            if (cart1.getGoodsNum() == 0) {
                cartMapper.delete(cart1);
            }
        }


    }
}
